home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / zkf102.zip / library.txt < prev    next >
Text File  |  1995-01-04  |  15KB  |  303 lines

  1. This document describes the classes and functions provided by the zk
  2. library.  These are handy because they are available immediately with
  3. no explicit #includes.  If any of the following descriptions are hard
  4. to understand at first, it might help to experiment with the classes
  5. and/or functions in question.  Using zk, such experimentation is much
  6. easier and faster than with a c++ compiler.
  7.  
  8. Text and integer container classes:
  9.  
  10.     String      --    Text.
  11.     Mfile       --    Two-dimensional text.
  12.     Iota        --    Groups of integers.
  13.     Iotasa      --    Two-dimensional groups of integers.
  14.  
  15. The relationship between String and Mfile is the same as the relationship
  16. between Iota and Iotasa.  An Mfile is a string of Strings, and an Iotasa
  17. is a string of Iotas.
  18.  
  19. In addition to the above classes, there is a "Z" version of each.  They
  20. are named String_Z, Mfile_Z, Iota_Z, and Iotasa_Z.  The "Z" versions are
  21. for moving the data efficiently, by disconnecting it from one object and
  22. reconnecting it to another object.  That makes compound expressions more
  23. efficient, because the data is not copied between subexpressions.
  24.  
  25. The operations you can do on the "Z" version of each class are limited.
  26. You can only use it as an argument to a function call, assignment, or
  27. initialization.  You can use it as the operand of "+=" and other operators
  28. because they are equivalent to function calls.  To retrieve the data
  29. from the "Z" object for further operations, you should assign it, using
  30. the assignment operator, "=", to an object of the corresponding non-"Z"
  31. class, and do the operations on that object.
  32.  
  33. In addition to the above classes, other similar classes are available
  34. if you enable them in sa.zh.  They are Mfilesa, Iotasasa, etc.  In each
  35. case, the suffix "sa" means it is a string of objects of that class.  So
  36. an Mfilesa is a string of Mfiles, to give 3-dimensional text, and an
  37. Iotasasa is a string of Iotasas, to give 3-dimensional groups of integers.
  38.  
  39. To convert an object to the corresponding "Z" class, use the "!" operator,
  40. which is a prefix operator.  No data is moved when you do that, but just
  41. relinked to its new owner.  See the examples below.
  42.  
  43. Here are the other operators you can use on the non-"Z" versions of all
  44. of the above classes:
  45.  
  46.    +=  Add new elements, individually or in groups.
  47.    ==  Compare, returns true if data is equal
  48.    !=  Compare, not equal
  49.    =   Assign data to the object, deleting old data.
  50.    []  Select an element from the object, return a reference
  51.  
  52.    See example usage of each operator below.
  53.  
  54. Here are the other functions you can use on the non-"Z" versions of all
  55. of the above classes:
  56.  
  57.    void compress(Iota&)               For each nonzero element of the
  58.                                       Iota, the corresponding element
  59.                                       of the object survives.  The rest
  60.                                       are deleted.
  61.  
  62.    void clear()                       Delete all the elements of the
  63.                                       object and reset it to empty.
  64.  
  65.    last()                             Returns a reference to the last
  66.                                       (rightmost) element of the object.
  67.  
  68.    offlensubstr(int offset, int length)  Makes a substring of the object,
  69.                                       from that offset and length.  The
  70.                                       result is of the corresponding "Z"
  71.                                       class (see above).
  72.  
  73.    ed and ted                         Edit and tail edit.  Ed takes 1, 2,
  74.                                       or 3 arguments.  The first argument
  75.                                       is the offset to the data to be
  76.                                       edited.  (Offset means element number,
  77.                                       starting from zero.)  The 2nd argument,
  78.                                       if any, is the number of elements to
  79.                                       delete.  The 3rd argument, if any, is
  80.                                       the data to insert in place of the
  81.                                       deleted data.  Often you will want to
  82.                                       delete 0 elements or insert 0 elements,
  83.                                       to just delete or insert instead of
  84.                                       replacing.
  85.  
  86.                                       Ted is similar to ed but operates on
  87.                                       the end of the string of data, and
  88.                                       just takes two arguments, with the
  89.                                       2nd argument optional.  The first
  90.                                       argument tells how many elements to
  91.                                       delete from the end, and the 2nd
  92.                                       tells what to replace them with.
  93.  
  94.                                       The replacement argument to ed and
  95.                                       ted, which tells what data to insert
  96.                                       in place of any data deleted, can be
  97.                                       of various types.  For each of the
  98.                                       classes discussed above, it can be
  99.                                       an element, or it can be a group of
  100.                                       elements, which means it's another
  101.                                       object of the same class as the object
  102.                                       being edited, or of the corresponding
  103.                                       "Z" class.  Also, for class String,
  104.                                       it can be an ordinary "char*" string.
  105.  
  106.    int len()                          Returns the number of elements.
  107.  
  108.    bool empty()                       Returns true if there are no elements.
  109.  
  110.    newlast()                          Adds an empty element to the end and
  111.                                       returns a reference to it.
  112.  
  113.  
  114. Member functions of String specifically, not available in the other classes:
  115.  
  116.    Note: The read and write functions return false when the operation fails.
  117.  
  118.    bool read()                        Read from standard input into the
  119.                                       String, until end of file is reached.
  120.                                       Newline characters go in the String.
  121.  
  122.    bool read("filename.ext")          Same as read, but from the given file.
  123.  
  124.    bool write()                       Write the String to standard output.
  125.  
  126.    bool write("filename.ext")         Write the String to the given file.
  127.  
  128.    ...
  129.  
  130.  
  131.  
  132. Besides the container classes, the zk library also provides the following
  133. miscellaneous functions, etc., some of which use the container classes:
  134.  
  135.    msg and gsm                         These macros output a formatted
  136.                                        message to standard error output,
  137.                                        which is normally the console, and
  138.                                        also to the session log, if any.
  139.                                        See environ.txt for details of how
  140.                                        to get a session log.  See examples
  141.                                        of msg/gsm in the examples below.
  142.                                        The format codes are %s for strings,
  143.                                        %d for decimal numbers, and %h for
  144.                                        hexadecimal numbers.
  145.  
  146.    write(Mfile_Z)                      This writes an entire Mfile to stanard
  147.                                        output, one line per String.
  148.  
  149.    write("filename",Mfile_Z)           Same as above, but to a file, replacing
  150.                                        the whole file.
  151.  
  152.    Mfile_Z exts(Mfile_Z)               This gets the filename extensions of
  153.                                        a list of filenames.
  154.  
  155.    Mfile_Z uniq(Mfile_Z)               This omits identical elements.
  156.  
  157.    Mfile_Z globm(cx)                   This gets filenames from a pattern,
  158.                                        e.g., "*.*" or "*.ini".
  159.  
  160.    Mfile_Z globmroots(cx)              Like globm but with the .extensions
  161.                                        deleted.
  162.  
  163.    Mfile_Z cols(int,int,Mfile_Z)       This converts an Mfile of
  164.                                        fields to a page of tabulated text.
  165.                                        The fields fill the page column by
  166.                                        column.  The page consists of lines
  167.                                        of text, one line per String of the
  168.                                        resulting Mfile.  The first argument
  169.                                        is the number of columns, and the
  170.                                        2nd is the width of each column.  No
  171.                                        tab characters are used, tabulation
  172.                                        is done by adding spaces.
  173.  
  174.    unsigned long mtime("filename")     This returns the modification date
  175.                                        and time of a file, such that you
  176.                                        can tell which file was modified
  177.                                        most recently by comparing their
  178.                                        mtimes.  Very handy for writing
  179.                                        your own version of make.
  180.  
  181.    whatis x                            This macro is useful mainly at the
  182.                                        zk command line.  It tells you what
  183.                                        zk thinks the name x means.  Handy
  184.                                        if you defined something and forget
  185.                                        whether it was a function or class
  186.                                        or what.
  187.  
  188.    void version()                      This outputs the version of zk.
  189.  
  190.    tx("prototype",args)                See msg/gsm above.  This provides
  191.                                        the same printf style processing,
  192.                                        but returns the result as char*,
  193.  
  194.  
  195. Non-class typedefs used by these functions:
  196.  
  197.    bool                                typedef int bool; 1=true, 2=false.
  198.    cx                                  typedef const char *cx;
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.                                  Examples
  207.  
  208.  
  209. Note: When these descriptions refer to classes such as Mfile, they can
  210. also mean Mfile_Z, etc., depending on which version the function takes.
  211. From the programmer's point of view, there is conceptually no difference
  212. between Mfile and Mfile_Z.  Mfile_Z is merely a vehicle for passing an
  213. Mfile to a function more efficiently.
  214.  
  215.  
  216. Example 1:  write(cols(20,4,uniq(sort(exts(globm("*.*"))))));
  217.  
  218. Example 1 looks at all the filenames in the current directory, gets the
  219. filename extension of each, sorts them, filters out duplicates, and
  220. writes the result in 20 4-character columns.  There will usually be less
  221. than 20 extensions, so the output will usually be one line, with just
  222. some of the 20 columns occupied.  The purpose of this is to show you all
  223. the filename extensions that exist in the current directory without
  224. requiring you to look at all the filenames.  If the output seems
  225. "tabulated", i.e., if the first 3-character extension output is blank,
  226. that means there are one or more filenames that have no extension at
  227. all, which is considered a blank extension.
  228.  
  229. Explanation of example 1:
  230. 1. The glom function creates an Mfile containing the filenames.
  231. 2. The exts function deletes the path and root filename from each filename
  232.    in the Mfile, leaving just the extension (e.g. ".exe").
  233. 3. The sort function sorts the results into alphabetical order.
  234. 4. The uniq function deletes duplications, so the word "exe", for example,
  235.    won't appear more than once in the output.
  236. 5. The cols function, with arguments 20 and 4, formats the names into
  237.    20 columns, each 4 characters wide, such that each String in the resulting
  238.    Mfile consists of one row of 20 columns.
  239. 6. The write function writes it to standard output so you can see it.
  240.  
  241.  
  242. Example 2:  write(cols(8,10,sort(globmroots("*.bat"))));
  243.  
  244. Example 2 looks for all batch files, *.bat, and writes their names,
  245. without the .bat extension, to standard output, in 8 columns, each
  246. column 10 characters wide.  The output is sorted in each column,
  247. continued in the next column.  The globmroots function finds all files
  248. that match the pattern and returns a list of their root names (without
  249. extensions).
  250.  
  251. Example 2 uses the same functions as example 1, except that it uses
  252. globmroots instead of globm.  The difference is that globmroots deletes
  253. the filename extensions, leaving the root filenames without extensions.
  254.  
  255.  
  256. Example 3:  msg "Hello world." gsm; // A newline character is appended too.
  257.  
  258.  
  259. Example 4:  msg "%s%d is A1","A",1 gsm; // This just shows how to use %s/%d.
  260.  
  261.  
  262. Example 5:  Mfile roots;
  263.             roots=globmroots("*.*"); // Mfile_Z is converted to Mfile.
  264.             roots=sort(!roots);      // And converted back to Mfile_Z and back.
  265.  
  266. Example 5 is just to show you how to use "!" to convert to a "Z" class, and
  267. the assignment operator to convert back to the non-"Z" class.  Keep in mind
  268. that the whole point of all these conversions is to avoid copying the data,
  269. to avoid taking extra time.  The conversions themselves take practically no
  270. time at all.
  271.  
  272.  
  273. Example 6:  String s1;
  274.             for(int i=0; i<10; i++) s1+="A";
  275.             msg "s1 is \"%s\"",s1 gsm;
  276.  
  277. Example 6 shows use of the += operator with the container classes.  Note
  278. that the backslashes in the string are for the purpose of making the
  279. quotation marks part of the string rather than indicating end of string.
  280. If this example is still not clear, it might help to copy it to the end
  281. of zk.ini so you can see it run.
  282.  
  283.  
  284. Example 7:  String s1="ABC";
  285.             if(s1=="xyz") msg "that's impossible" gsm;
  286.             if(s1=="ABC") msg "that's much better" gsm;
  287.             if(s1!="ABC") msg "forget it, too many contradictions" gsm;
  288.  
  289. Example 7 shows that you can use char* literals with String::operator==
  290. and String::operator!=.  You can also use other Strings or String_Z's.
  291.  
  292.  
  293. Example 8:  String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  294.             for(int i=0,j=alphabet.len(); i<j; i++) msg "-%s-",alphabet[i] gsm;
  295.  
  296. Example 8 shows usage of the len() function and the [] operator.
  297.  
  298.  
  299. If any of the examples are not clear, copy them to the end of zk.ini so you
  300. can see them run.  If you are already in zk, just type "lz" at the zk
  301. command line prompt to reload zk.ini, which will run the example you copied.
  302.  
  303.